extends BallState

# ===============================================

const OFFSET_FROM_PLAYER: Vector2 = Vector2(10, 4)

const DRIBBLE_FREQUENCY: float = 10.0
const DRIBBLE_INTENSITY: float = 3.0

# ===============================================

var dribble_time: float = 0.0

# ===============================================

func init() -> void:
	ball.shoot.connect(
		func(shoot_velocity: Vector2):
			ball.velocity = shoot_velocity
			state_transition.emit(self, State.SHOT)
	)


# TODO 不明bug: 单独加vx偏移量，结果错误
func process(delta: float) -> void:
	# 动画
	if ball.carrier.velocity == Vector2.ZERO:
		ball.animation_player.play("idle")
	else:
		if ball.carrier.heading.x >= 0:
			ball.animation_player.play("roll")
		else:
			ball.animation_player.play_backwards("roll")
		ball.animation_player.advance(0)
	# 水平方向震荡效果
	dribble_time += delta
	var vx: float = 0.0
	if ball.carrier.velocity.x != 0:
		vx = cos(dribble_time * DRIBBLE_FREQUENCY) * DRIBBLE_INTENSITY
	# 跟随角色移动
	ball.position = ball.carrier.position + Vector2(vx + ball.carrier.heading.x * OFFSET_FROM_PLAYER.x, OFFSET_FROM_PLAYER.y)


func exit() -> void:
	ball.carrier = null
